﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace UFSSWebApp
{
    public partial class popusers : System.Web.UI.Page
    {
        UFSSservicereference.UserCreds uc = new UFSSservicereference.UserCreds();
        UFSSservicereference.UFSSserviceSoapClient ussc = new UFSSservicereference.UFSSserviceSoapClient();
        protected void Page_Load(object sender, EventArgs e)
        {
            uc = (UFSSservicereference.UserCreds)Session["usercreds"];
            try
            {
                refresh();
                lblError.Visible = false;
            }
            catch
            {
                lblError.Visible = true;
                lblError.Text = "There was a problem completing your request... please try again later";
            }
            refresh();
        }
        private void refresh()
        {

            UFSSservicereference.UFSSUser[] users = ussc.getTopUsers(uc, 10);

            //create dataset
            DataSet ds = new DataSet();
            DataTable dt = new DataTable("users");
            DataRow dr;

            //create columns
            DataColumn namecolumn, filescolumn;
            namecolumn = new DataColumn("Username");
            filescolumn = new DataColumn("Files");





            //insert columns

            dt.Columns.Add(namecolumn);
            dt.Columns.Add(filescolumn);



            //create headers

            //fill table
            foreach (UFSSservicereference.UFSSUser user in users)
            {
                dr = dt.NewRow();
                dr["Username"] = user.username;
                dr["Files"] = user.filecount;


                dt.Rows.Add(dr);
            }
            ds.Tables.Add(dt);

            //populate gridview
            DataView dv = new DataView(ds.Tables["users"]);
            dv = ds.Tables[0].DefaultView;


            gvPopUsers.DataSource = ds.Tables["users"];
            gvPopUsers.AutoGenerateColumns = true;
            gvPopUsers.DataBind();
            for (int i = 0; i < users.Length; i++)
            {
                gvPopUsers.Rows[i].Cells[0].Text = "<a href=\"userpage.aspx?id=" + users[i].username + "\">" + users[i].username + "</a>";
                
            }
        }
    }
}